package cn.yx.tool.message.service;

import cn.yx.common.core.annotation.message.English;
import cn.yx.common.core.annotation.message.I18nMessage;
import cn.yx.common.core.annotation.message.SimplifiedChinese;
import cn.yx.common.core.constant.FileConstant;
import cn.yx.common.core.util.io.FileUtil;
import cn.yx.common.core.util.io.IoUtil;
import cn.yx.common.core.util.reflect.ClassUtil;
import cn.yx.tool.message.properties.MessageGeneratorProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

/**
 * <p>国际化消息SQL生成服务</p>
 *
 * @author Wgssmart
 */
@Service
@Slf4j
public class MessageGeneratorService {

    private final MessageGeneratorProperties messageGeneratorProperties;

    public MessageGeneratorService(MessageGeneratorProperties messageGeneratorProperties) {
        this.messageGeneratorProperties = messageGeneratorProperties;
    }

    /**
     * 根据国际化消息常量类指定的注解生成insert sql文件
     */
    public void generateMessageSql() {
        String dir = FileUtil.mkdirs(messageGeneratorProperties.getBaseDir() + messageGeneratorProperties.getSaveDir());
        for (String jarFilePath : messageGeneratorProperties.getJarFilePath()) {
            File jarFile = new File(messageGeneratorProperties.getBaseDir() + jarFilePath);
            List<Class<?>> classList = ClassUtil.getClassesFromOutJar(jarFile, I18nMessage.class);
            for (Class<?> clazz : classList) {
                StringBuilder sqlBuilder = new StringBuilder();
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    sqlBuilder.append(createSql(field));
                }
                IoUtil.writeText(sqlBuilder.toString(), dir + FileConstant.SEPARATOR + clazz.getSimpleName() + ".sql");
            }
        }
    }

    /**
     * 把国际化常量消息key生成sql语句
     *
     * @param field
     * @return
     */
    private String createSql(Field field) {
        SimplifiedChinese simplifiedChinese = field.getAnnotation(SimplifiedChinese.class);
        English english = field.getAnnotation(English.class);
        try {
            String cnSql = "";
            String enSql = "";
            if (Objects.nonNull(simplifiedChinese)) {
                cnSql = messageGeneratorProperties.getSqlTemplate()
                        .replaceAll(":key", "'" + field.get(null).toString() + "'")
                        .replaceAll(":lang", "'zh_CN'")
                        .replaceAll(":message", "'" + simplifiedChinese.value() + "'")
                        + ";\n";
            }
            if (Objects.nonNull(english)) {
                enSql = messageGeneratorProperties.getSqlTemplate()
                        .replaceAll(":key", "'" + field.get(null).toString() + "'")
                        .replaceAll(":lang", "'en_US'")
                        .replaceAll(":message", "'" + english.value() + "'")
                        + ";\n";
            }
            return cnSql + enSql;
        } catch (IllegalAccessException e) {
            log.error("create sql error: {}", e.getMessage(), e);
            return "";
        }
    }

}
